package com.example.projectsharedspace;

import java.util.Calendar;

/**
 * This class resembles a poll. It keeps track of what each user has answered.
 * 
 * @author Jan Ove
 * @since 2013-10-25
 */
public class Poll extends Message {
	
	private Vote[] votes;
	private int yesCount = 0;
	private int noCount = 0;
	
	private final static int VIEW_TYPE = 1;
	
			
	public Poll(String msg, Calendar td, User u) {
		super( msg,td,u);
		votes = (Vote[]) super.getUser().getHome().getPeopleLivingHere().toArray();
		
		for(int i = 0; i < u.getHome().getPeopleLivingHere().size(); i++) {
			votes[i] = new Vote(u, false);
		}
		// The creators vote should be yes.
		changeVote(u, true);
	}
	
	/** Internal class for keeping track of votes related to users*/
	private class Vote {
		public Vote(User u, boolean ans) {
			this.u = u;
			this.ans = ans;
		}
		User u;
		boolean ans = false;
	}
	
	/**
	 * Changes a vote in the poll.
	 * 
	 * @param u	the user whose answer is to be changed (or not, depending on his previous answer).
	 * @param ans the answer of the vote. true resembles "yes" and counts twoward the yesCount, and vice-versa for false.
	 */
	public void changeVote(User u, boolean ans) {
		for(int i = 0; i < votes.length; i++) {
			if(u == votes[i].u) {
				votes[i].ans = ans;
				if(ans) {
					yesCount++;
				} else {
					noCount++;
				}
				break;
			}
		}
	}
	 /**
	  * @return the amount of positive votes.
	  */
	public int getYesCount() {
		return yesCount;
	}
	/**
	 * @return the amount of negative votes.
	 */
	public int getNoCount() {
		return noCount;
	}
	
	@Override
	public int getViewType() {
		return VIEW_TYPE;
	}
	
}
